using System;

public class ExcelRow
{
    public int Id { get; set; }  // 行号
    public string Property1 { get; set; }  // A列
    public string Property2 { get; set; }  // B列
    public string Property3 { get; set; }  // C列
    public string Property4 { get; set; }  // D列
    public string Property5 { get; set; }  // E列
    public double[] Property6 { get; set; } = new double[12];  // G-R列（12个）
    public string Property7 { get; set; }  // AD列
    public string Property8 { get; set; }  // AE列
    public string Property9 { get; set; }  // AF列

    public double SumProperty6(int start, int end)
    {
        if (start < 0 || end >= Property6.Length || start > end)
            throw new ArgumentException("Invalid range");

        double sum = 0;
        for (int i = start; i <= end; i++)
        {
            sum += Property6[i];
        }
        return sum;
    }
}

public class ExcelReader
{
    public static List<ExcelRow> ReadExcel(string filePath)
    {
        var rows = new List<ExcelRow>();

        using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            var workbook = WorkbookFactory.Create(fs);
            var sheet = workbook.GetSheetAt(0);

            for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++)  // 从第2行开始
            {
                var row = sheet.GetRow(rowNum);
                if (row == null) continue;

                var excelRow = new ExcelRow
                {
                    Id = rowNum,
                    Property1 = GetCellString(row, 0),   // A列
                    Property2 = GetCellString(row, 1),   // B列
                    Property3 = GetCellString(row, 2),   // C列
                    Property4 = GetCellString(row, 3),   // D列
                    Property5 = GetCellString(row, 4),   // E列
                    Property7 = GetCellString(row, 29),  // AD列（第30列）
                    Property8 = GetCellString(row, 30),  // AE列
                    Property9 = GetCellString(row, 31)   // AF列
                };

                // 读取G-R列（6-17列）
                for (int i = 6; i <= 17; i++)
                {
                    excelRow.Property6[i - 6] = GetCellDouble(row, i);
                }

                rows.Add(excelRow);
            }
        }

        return rows;
    }

    private static string GetCellString(IRow row, int column)
        => row.GetCell(column)?.ToString() ?? string.Empty;

    private static double GetCellDouble(IRow row, int column)
        => double.TryParse(row.GetCell(column)?.ToString(), out var result) ? result : 0;
}

 public class ExcelWriter
{
    public static void WriteExcel(string filePath, List<ExcelRow> rows)
    {
        IWorkbook workbook = new XSSFWorkbook(); // 创建xlsx格式工作簿
        ISheet sheet = workbook.CreateSheet("Sheet1");

        // 创建表头（可根据需要自定义）
        IRow headerRow = sheet.CreateRow(0);
        string[] headers = {
            "A列", "B列", "C列", "D列", "E列",
            "", "", // F列占位
            "G列", "H列", "I列", "J列", "K列", "L列", "M列",
            "N列", "O列", "P列", "Q列", "R列",
            "", "", "", "", "", "", "", "", "", "", // S-AC列占位
            "AD列", "AE列", "AF列"
        };
        for (int i = 0; i < headers.Length; i++)
        {
            headerRow.CreateCell(i).SetCellValue(headers[i]);
        }

        // 写入数据行
        for (int i = 0; i < rows.Count; i++)
        {
            IRow dataRow = sheet.CreateRow(i + 1); // 数据从第2行开始
            var rowData = rows[i];

            // A-E列
            dataRow.CreateCell(0).SetCellValue(rowData.Property1);
            dataRow.CreateCell(1).SetCellValue(rowData.Property2);
            dataRow.CreateCell(2).SetCellValue(rowData.Property3);
            dataRow.CreateCell(3).SetCellValue(rowData.Property4);
            dataRow.CreateCell(4).SetCellValue(rowData.Property5);

            // G-R列（索引6-17）
            for (int j = 0; j < 12; j++)
            {
                dataRow.CreateCell(j + 6).SetCellValue(rowData.Property6[j]);
            }

            // AD-AF列（索引29-31）
            dataRow.CreateCell(29).SetCellValue(rowData.Property7);
            dataRow.CreateCell(30).SetCellValue(rowData.Property8);
            dataRow.CreateCell(31).SetCellValue(rowData.Property9);
        }

        // 自动调整列宽（可选）
        for (int i = 0; i < headers.Length; i++)
        {
            sheet.AutoSizeColumn(i);
        }

        // 保存文件
        using (var fs = new FileStream(filePath, FileMode.Create))
        {
            workbook.Write(fs);
        }
    }
}